<template>
	<view class="index-content">
		<u-navbar bgColor="#2b85e4" :placeholder="true">
			<view slot="left" @click="onRefreshBtn">
                <u--image src="/static/logo/hy.png" width="48px" height="30px"></u--image>
            </view>
			<view slot="center" style="color: #fff;font-size: 34rpx;">{{ customerName }}</view>
		</u-navbar>
        <u-swiper
            keyName="image"
            :height="240"
            :list="swiperList"
        ></u-swiper>
		<!--<view class="nav-bar">
			<view class="nav-bar-item" v-for="(item, index) in navBarList" :key="index" @click="onJump(item)">
				<u-icon :name="item.icon" :color="item.bgColor" size="32px"></u-icon>
				<view class="nav-bar-item-name">{{ item.name }}</view>
			</view>
		</view>-->
        <view v-show="noticeText" class="margin-top-10">
            <u-notice-bar :text="noticeText" type="danger" :fontSize="16"></u-notice-bar>
        </view>
		<view class="menu-box">
			<view class="menu-box-title">
				<view class="menu-box-icon"></view>
				<view class="menu-box-name">菜单</view>
			</view>

			<view class="menu-box-content">
				<view v-for="(item) in menuList"
                      :key="item.name"
                      @click="onJump(item)"
                      class="menu-box-item"
                >
					<view class="menu-box-item-inner">
						<view :style="{ background: item.bgColor }" class="menu-box-item-inner-left">
							<u-icon :name="item.icon" color="#fff" size="28px"></u-icon>
						</view>
						<view class="menu-box-item-inner-right">{{ item.name }}</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
    import {
        systemAuthGetPermissionInfo,
        systemAuthListLeftMenus, systemNoticeShow
    } from "../../api";
    import {showLoading} from "../../utils/jk-utils";
    import permission from "../../utils/permission";
    import {env} from "../../utils/env";

	export default {
	    mixins: [permission],
		data() {
			return {
                customerName: env.name,
                swiperList: [
                    {
                        image: 'https://spin.jeacon.cn/image/1.jpg',
                        title: '昨夜星辰昨夜风，画楼西畔桂堂东',
                    },{
                        image: 'https://spin.jeacon.cn/image/2.png',
                        title: '身无彩凤双飞翼，心有灵犀一点通'
                    },{
                        image: 'https://spin.jeacon.cn/image/3.png',
                        title: '谁念西风独自凉，萧萧黄叶闭疏窗，沉思往事立残阳'
                    }
                ],
				workshopList: [],
                noticeText: '',
                menuList: [],
                categoryList: [
                    {
                        name: '生产管理',
                        path: '/pages/production/grid/index',
                        icon: 'order',
                        bgColor: 'rgb(21, 166, 87)',
                        pcRouterPath: ['/production']
                    },
                    {
                        name: '工艺管理',
                        path: '/pages/technology/task/index',
                        icon: 'attach',
                        bgColor: '#2d8cf0',
                        pcRouterPath: ['/sheet']
                    },
                    {
                        name: '生产监控',
                        path: '/pages/monitorCenter/grid/index',
                        icon: 'warning',
                        bgColor: 'rgb(232, 87, 83)',
                        pcRouterPath: ['/productionMonitor']
                    },
                    {
                        name: '排班管理',
                        path: '/pages/schedule/grid/index',
                        icon: 'calendar',
                        bgColor: 'rgb(129, 65, 217)',
                        pcRouterPath: ['/schedule']
                    },
                    {
                        name: '维保中心',
                        path: '/pages/upkeepAndRepair/repair/grid/index',
                        icon: 'setting',
                        bgColor: 'rgb(21, 166, 87)',
                        pcRouterPath: ['/machine']
                    },
                    {
                        name: '报表中心',
                        path: '/pages/reports/grid/index',
                        icon: 'pushpin-fill',
                        bgColor: '#ff9900',
                        pcRouterPath: ['/statisticAnalysis', '/salaryStatistics']
                    },
                ],
                navBarList: [{
						name: '通知',
						path1: '/pages/warning/machineWarning/index',
						icon: 'warning',
						bgColor: 'rgb(232, 148, 48)'
					},
					{
						name: '人机绑定',
						path1: '/pages/schedule/empBindMachineBatch/index',
						icon: 'man-add-fill',
						bgColor: '#2d8cf0'
					},
					{
						name: '品种开台',
						path1: '/pages/production/openMachine/index',
						icon: 'play-circle',
						bgColor: 'rgb(21, 166, 87)'
					},
					{
						name: '品种了机',
						path1: '/pages/production/closeMachine/index',
						icon: 'pause-circle',
						bgColor: 'rgb(129, 65, 217)'
					},
				],
                isAdmin: false
			}
		},
		onLoad() {
            wx.showShareMenu({
                withShareTicket:true,
                //设置下方的Menus菜单，才能够让发送给朋友与分享到朋友圈两个按钮可以点击
                menus:["shareAppMessage","shareTimeline"]
            })
		},
		onShow() {
		    this.getUpdateVersion();
            this.getDependentData();
		},
		methods: {
            onRefreshBtn() {
                showLoading(true);
                this.getDependentData();
            },
		    getUpdateVersion() {
                const updateManager = wx.getUpdateManager()
                updateManager.onCheckForUpdate(function (res) {
                    // 请求完新版本信息的回调
                })

                updateManager.onUpdateReady(function () {
                    wx.showModal({
                        title: '更新提示',
                        content: '新版本已经准备好，是否重启应用？',
                        success: function (res) {
                            if (res.confirm) {
                                // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                                updateManager.applyUpdate()
                            }
                        }
                    })
                })

                updateManager.onUpdateFailed(function () {
                    // 新版本下载失败
                })
            },
            getUserMenuListRequest() {
                return systemAuthListLeftMenus().then(res => {
                    if (res.data.status === 200) {
                        this.getFlattenData(res.data.res || []);
                    }
                });
            },
            // 获取将树状数据结构转为扁平化数据结构
            getFlattenData(data = []) {
                let arr = [];
                function formatData(data) {
                    data.forEach(x => {
                        arr.push({
                            id: x.id,
                            name: x.name,
                            path: x.path,
                            parentId: x.parentId
                        });
                        if (x.children && x.children.length) {
                            formatData(x.children);
                        }
                    });
                }
                formatData(data);
                getApp().globalData.userMenuList = arr;
                this.menuList = this.categoryList.filter(x => {
                    return this.getMenuItemPermission(x);
                });
            },
            getNoticeMsgRequest() {
                return systemNoticeShow().then(res => {
                    if (res.data.status === 200) {
                        this.noticeText = res.data.res ? res.data.res.map(x => x.content.replace(/\n/g, '')).join(';') : '';
                    }
                });
            },
			async getDependentData() {
                await this.getNoticeMsgRequest();
                await this.getUserMenuListRequest();
				await this.getWorkshopDataRequest();
                showLoading(false);
            },
			getWorkshopDataRequest() {
				return systemAuthGetPermissionInfo().then(res => {
                    showLoading(false);
                    if (res.data.status === 200) {
                        this.workshopList = res.data.res.workshop.workshops;
                        getApp().globalData.roles = res.data.res.roles;
                        getApp().globalData.defaultWorkshop.id = res.data.res.workshop.defaultWorkshopId;
						getApp().globalData.defaultWorkshop.code = res.data.res.workshop.defaultWorkshopCode;
						getApp().globalData.defaultWorkshop.name = res.data.res.workshop.defaultWorkshopName;
						getApp().globalData.defaultWorkshop.list = res.data.res.workshop.workshops.map(x => {
                            x.id = x.workshopId;
                            x.code = x.workshopCode;
                            x.name = x.workshopName;
                            return x;
                        });
                        getApp().globalData.defaultEmp = {
                            ...res.data.res.emp,
                            id: res.data.res.emp && res.data.res.emp.id ? res.data.res.emp.id : '',
                            code: res.data.res.emp && res.data.res.emp.code ? res.data.res.emp.code : '',
                            name: res.data.res.emp && res.data.res.emp.name ? res.data.res.emp.name : ''
                        };
                        getApp().globalData.defaultProcess = {
                            defaultProcessId: res.data.res.process.defaultProcessId, // 逐步废弃
                            defaultProcessName: res.data.res.process.defaultProcessCode, // 逐步废弃
                            defaultProcessCode: res.data.res.process.defaultProcessName, // 逐步废弃
                            processList: res.data.res.process.processList,
                            id: res.data.res.process.defaultProcessId,
                            code: res.data.res.process.defaultProcessCode,
                            name: res.data.res.process.defaultProcessName,
                            list: res.data.res.process.processList.map(x => {
                                x.id = x.processId;
                                x.code = x.processCode;
                                x.name = x.processName;
                                return x;
                            })
                        };
					}
				})
			},
			onJump({path}) {
				if (path) {
					wx.navigateTo({
						url: path
					});
				} else {
					wx.showToast({
						title: '敬请期待',
						icon: 'error'
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
    @import "./index";
</style>
